#!/bin/sh
# -*- tcl -*-
# The next line is executed by /bin/sh, but not tcl \
exec tclsh "$0" ${1+"$@"}

#
# Check that ^ and $ are only special at the start and end of a pattern.
#
# This is done in mapstring(), which converts all other forms to egrep-style
# regular expressions.  If regexptype=egrep, they should be recognised at
# the start and end of pattern alternatives too.
#
# Was https://github.com/martinwguy/xvi/issues/138
#

source scripts/term
start_vi

set pattern  [list "aaa^aaa" "bbb\$bbb"]

# First, check the actual defect that brought us here;
# the inability to search for " ^ "
test 10 "aaaa^aaa\rbbb\$bbb[esc]0"	2 0 $pattern
test 11 "/a^a/\r"		1 2 $pattern

# Check it works at start of line unless quoted
test 20 "/^b\r"			2 0 $pattern
test 21 "/\\^a\r"		1 3 $pattern

exp_send ":se regextype=egrep\r"

# and at start of alternative patterns in egrep
test 31 "/(^a|nothing)\r"	1 0 $pattern
test 32 "/(nothing|^b)\r"	2 0 $pattern
# and that it is not special when the alt-pattern delimiters are quoted
exp_send "Gonothing|^b[esc]1G"
test 33 "/(nothing\\|^b)\r"	3 0 [list "aaa^aaa" "bbb\$bbb" "nothing|^b"]
exp_send "GC(^b|nothing)[esc]1G"
test 34 "/\\(^b|nothing\\)\r"	3 0 [list "aaa^aaa" "bbb\$bbb" "(^b|nothing)"]

exp_send ":se regextype=grep\r"
test 39 "1GdGaaaa^aaa\rbbb\$bbb[esc]0"	2 0 $pattern

# Similar checks for $
# Check it works at end of line 
test 40 "1G/b\$\r"		2 6 $pattern
test 41 "1G/b\\$\r"		2 2 $pattern

exp_send ":se regextype=egrep\r"

# and at end of alternative patterns in egrep.
# test 52 just serves to avoid having two successive tests with the same result
test 51 "1G/(b\$|nothing)\r"	2 6 $pattern
test 52 "1G"			1 0 $pattern
test 53 "/(nothing|b\$)\r"	2 6 $pattern

stop_vi

exit 0
